<template>
    <div>
        <el-dialog top="5%" :title="title" size="large" v-model="editFormVisible" :close-on-click-modal="false">
           <Approve @onApproveSuc="onApproveSuc" :showcontent="showcontent" ref="approve" v-if="ApproveID"></Approve>
            <el-tag v-if="ApproveID"  style="font-size: larger; margin: 30px 0px 10px 20px;">施工费用详情</el-tag>
            <div style="border: 1px solid #bbb; padding: 20px;">
            <el-form  ref="form" :model="form" :inline="true" :rules="rules" label-width="150px">
                <el-form-item label="编号">
                    <el-input disabled  v-model="form.FNo" style="width:250px;"></el-input>
                </el-form-item>
                <el-form-item label="申请时间">
                    <el-date-picker type="date"  v-model="form.FApplyDate"  style="width:250px"></el-date-picker>
                </el-form-item>
                <el-form-item label="项目" placeholder="请选择"  prop="FProjectID" >
                    <sy-select v-model="form.FProjectID"  valueKey="FID" style="width:250px" @change="changeproject"
                               optionData="server/setting/Project.ashx?cmd=GetOptions">
                    </sy-select>
                </el-form-item>
                <el-form-item label="施工队" placeholder="请选择"  prop="FTeamID">
                    <sy-select v-model="form.FTeamID"  valueKey="FID" style="width:250px"
                               optionData="server/business/ConstructionTeam.ashx?cmd=GetOptions">
                    </sy-select>
                </el-form-item>
            </el-form>
                <el-tag style="font-size: larger; margin: 30px 0px 10px 20px;">工程费用</el-tag>
            <el-table
                    :data="listdata"
                    border
                    v-loading="listLoading"
                    style="width: 100%">
                <el-table-column
                        prop="FSubitemID"
                        label="单项工程"
                        width="280">
                    <template scope="scope">
                        <sy-select v-if="scope.row.FSubitemID!=-1" v-model="scope.row.FSubitemID" valueKey="FID" @change="changesubitem" :extendData="scope.row"
                                   :optionData="'server/business/Subitem.ashx?cmd=GetOptions1&FProjectID='+form.FProjectID+'&where='+subitemwhere">
                        </sy-select>
                        <label v-else>单价</label>
                    </template>
                </el-table-column>
                <el-table-column
                        v-for="(item,index) in columnslist"
                        :prop="'FItem'+(index+1)"
                        :label="item.FName"
                        width="120">
                    <template scope="scope">
                        <el-input type="number"  v-model.number="scope.row['FItem'+(index+1)]"></el-input>
                    </template>
                </el-table-column>
                <el-table-column
                  prop=""
                  label="操作">
                 <template scope="scope">
                 <a  href="javascript:;" @click="addRow"><i style="color:#8391a5" class="el-icon-plus"></i></a>
                 <a style="margin-left: 10px;"  href="javascript:;" @click="delRow(scope.$index)"><i style="color:#8391a5" class="el-icon-delete"></i></a>
                 </template>
                </el-table-column>
            </el-table>
                <el-tag style="font-size: larger; margin: 30px 0px 10px 20px;">增补费用</el-tag>
                <div>
                <el-table
                        :data="listsupple0"
                        border
                        v-loading="listLoading"
                        style="width: 45%;float: left">
                    <el-table-column
                            prop="FName"
                            label="增补项"
                            width="200">
                        <template scope="scope">
                            <el-input v-model="scope.row.FName"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column
                            prop="FCost"
                            label="金额"
                            width="200">
                        <template scope="scope">
                            <el-input type="number"  v-model.number="scope.row.FCost"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column
                            prop=""
                            label="操作">
                        <template scope="scope">
                            <a  href="javascript:;" @click="addsupple0Row"><i style="color:#8391a5" class="el-icon-plus"></i></a>
                            <a style="margin-left: 10px;"  href="javascript:;" @click="delsupple0Row(scope.$index)"><i style="color:#8391a5" class="el-icon-delete"></i></a>
                        </template>
                    </el-table-column>
                </el-table>
                <el-table
                        :data="listsupple1"
                        border
                        v-loading="listLoading"
                        style="width:49%;float: left;margin-left:50px;">
                    <el-table-column
                            prop="FName"
                            label="扣减项"
                            width="200">
                        <template scope="scope">
                            <el-input v-model="scope.row.FName"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column
                            prop="FCost"
                            label="金额"
                            width="200">
                        <template scope="scope">
                            <el-input type="number"  v-model.number="scope.row.FCost"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column
                            prop=""
                            label="操作">
                        <template scope="scope">
                            <a  href="javascript:;" @click="addsupple1Row"><i style="color:#8391a5" class="el-icon-plus"></i></a>
                            <a style="margin-left: 10px;"  href="javascript:;" @click="delsupple1Row(scope.$index)"><i style="color:#8391a5" class="el-icon-delete"></i></a>
                        </template>
                    </el-table-column>
                </el-table>
                    </div>
            <div style="text-align: right; font-size:larger;color:red;margin-right:100px;margin-top:10px;">总费用：{{totalprice}}元,已付：{{form.FAccountCost}}元</div>
            </div>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="editFormVisible = false">取 消</el-button>
                <el-button type="primary" v-if="showSave" @click="save" :loading="loading">保存</el-button>
            </div>
      </el-dialog>
    </div>
</template>

<script>
    import util from '../../common/util'
    import mixin from '../../common/mixin'
    import Approve from './Approve.vue'
    import Vue from 'vue'
    export default {
        components:{
            Approve:Approve
        },
        data() {
            return {
                form: {},
                showSave:true,
                rules: {
                   // FName: [{type: "date",name:"FCreateDate",text:"跟进时间", value: null}]
                },
                loading:false,
                editFormVisible:false,
                title:'',
                listdata: [],
                listsupple1:[],
                listsupple0:[],
                listLoading:false,
                ApproveID:null,
                showcontent:true,
                fixWhere:'',
                columnslist:[]
            }
        },
        computed: {
            subitemwhere: function () {
               let result=[]
               for(let pro of this.listdata){
                    if(pro.FSubitemID) {
                        result.push(pro.FSubitemID)
                    }
               }
               if(result.length==0)
               {
                   return ""
               }
               return ','+result.join(',')+','
            },
            totalprice:function(){
                let result=0;
                let item1singleprice=0
                let item2singleprice=0
                let item3singleprice=0
                let item4singleprice=0
                let item5singleprice=0
                let item6singleprice=0
                let item7singleprice=0
                let item8singleprice=0
                let item9singleprice=0
                let item10singleprice=0
                for(let pro of this.listdata){
                    if(pro.FSubitemID==-1){
                        if(pro.FItem1){ item1singleprice= pro.FItem1} else {item1singleprice= 0}
                        if(pro.FItem2){ item2singleprice= pro.FItem2} else {item2singleprice= 0}
                        if(pro.FItem3){ item3singleprice= pro.FItem3} else {item3singleprice= 0}
                        if(pro.FItem4){ item4singleprice= pro.FItem4} else {item4singleprice= 0}
                        if(pro.FItem5){ item5singleprice= pro.FItem5} else {item5singleprice= 0}
                        if(pro.FItem6){ item6singleprice= pro.FItem6} else {item6singleprice= 0}
                        if(pro.FItem7){ item7singleprice= pro.FItem7} else {item7singleprice= 0}
                        if(pro.FItem8){ item8singleprice= pro.FItem8} else {item8singleprice= 0}
                        if(pro.FItem9){ item9singleprice= pro.FItem9} else {item9singleprice= 0}
                        if(pro.FItem10){ item10singleprice= pro.FItem10} else {item10singleprice= 0}
                   }
                   else{
                        if(pro.FItem1&&item1singleprice!=0) { result = result+ (pro.FItem1*item1singleprice)}
                        if(pro.FItem2&&item2singleprice!=0) { result = result+ (pro.FItem2*item2singleprice)}
                        if(pro.FItem3&&item3singleprice!=0) { result = result+ (pro.FItem3*item3singleprice)}
                        if(pro.FItem4&&item4singleprice!=0) { result = result+ (pro.FItem4*item4singleprice)}
                        if(pro.FItem5&&item5singleprice!=0) { result = result+ (pro.FItem5*item5singleprice)}
                        if(pro.FItem6&&item6singleprice!=0) { result = result+ (pro.FItem6*item6singleprice)}
                        if(pro.FItem7&&item7singleprice!=0) { result = result+ (pro.FItem7*item7singleprice)}
                        if(pro.FItem8&&item8singleprice!=0) { result = result+ (pro.FItem8*item8singleprice)}
                        if(pro.FItem9&&item9singleprice!=0) { result = result+ (pro.FItem9*item9singleprice)}
                        if(pro.FItem10&&item10singleprice!=0) { result = result+ (pro.FItem10*item10singleprice)}
                    }
               }
               for(let pro of this.listsupple0){
                   if(pro.FCost){ result = result + pro.FCost;}
               }
               for(let pro of this.listsupple1){
                   if(pro.FCost){ result = result - pro.FCost;}
               }
                return result
            }

        },
        methods: {
            switchLoading(flag){
                this.loading = flag;
            },
            _openEdit(record) {
                this.editFormVisible = true;
                this.form = Object.assign({},record);
                this.title = '编辑'
                util.get("server/business/Single.ashx?cmd=GetList",
                    {where:"FProjectID="+this.form.FProjectID,start:"0",limit:'10'},this.switchLoading,result=>{
                        this.columnslist = result.data
                    })
                util.get("server/business/ConscostDetail.ashx?cmd=GetList",
                    {where:"FMainID="+this.form.FID},this.switchLoading,result=>{
                        this.listdata = result.listdata
                    })
                util.get("server/business/ConscostSupple.ashx?cmd=GetList",
                    {where:"FMainID="+this.form.FID},this.switchLoading,result=>{
                    for(let item of result.listdata) {
                        if (item.FType == 1) {
                            this.listsupple1.push(item)
                        }
                        else {
                            this.listsupple0.push(item)
                        }
                    }
                    if(this.listsupple1.length==0){ this.listsupple1.push({FID: '0',FName:'',FCost: '',FType:'1'})}
                    if(this.listsupple0.length==0){ this.listsupple0.push({FID: '0',FName:'',FCost: '',FType:'0'})}
                    })
            },
            _openAdd(record) {
                this.editFormVisible = true;
                this.form = {FID:0,FNo:'',FApplyDate:'',FProjectID:'',
                    FCreateDate:new Date(),FCreateUserID:global.user.FID,listdata:[],FApproveStatus:0,FNeedCost:0};
                this.title = '新增'
                this.listdata.push({FSubitemID: '-1',FItem1:'',FItem2: '',FItem3:'',FItem4:'',FItem5:'',
                    FItem6:'',FItem7:'',FItem8:'',FItem9:'',FItem10:''})
                this.listsupple1.push({FID: '0',FName:'',FCost: '',FType:'1'})
                this.listsupple0.push({FID: '0',FName:'',FCost: '',FType:'0'})
                util.get("server/business/ConstructionCost.ashx?cmd=GetNo",
                    null,this.switchLoading,result=>{
                       this.form.FNo = result.no
                   })
            },
            _openApprove(record,ApproveID) {
                this.editFormVisible = true;
                this.ApproveID = ApproveID
                this.form = Object.assign({},record);
                if(record.FApproveStatus!=0&&record.FApproveStatus!=-1){
                if((record.FToApprover!=global.user.FID)||record.FApproveStatus==3)
                {
                    this.showSave=false
                    this.showcontent=false
                    this.title='查看'
                }
                else {
                    this.title='审核'
                }
                }
                if(record.FApproveStatus==0||record.FApproveStatus==-1) {
                    this.title='提交审核'
                }

                util.get("server/business/Single.ashx?cmd=GetList",
                    {where:"FProjectID="+this.form.FProjectID,start:"0",limit:'10'},this.switchLoading,result=>{
                        this.columnslist = result.data
                    })
                util.get("server/business/ConscostDetail.ashx?cmd=GetList",
                    {where:"FMainID="+this.form.FID},this.switchLoading,result=>{
                        this.listdata = result.listdata
                    })
                util.get("server/business/ConscostSupple.ashx?cmd=GetList",
                    {where:"FMainID="+this.form.FID},this.switchLoading,result=>{
                        for(let item of result.listdata) {
                            if (item.FType == 1) {
                                this.listsupple1.push(item)
                            }
                            else {
                                this.listsupple0.push(item)
                            }
                        }
                        if(this.listsupple1.length==0){ this.listsupple1.push({FID: '0',FName:'',FCost: '',FType:'1'})}
                        if(this.listsupple0.length==0){ this.listsupple0.push({FID: '0',FName:'',FCost: '',FType:'0'})}
                    })
                this.$nextTick(()=>{
                    this.$refs.approve._init(record,ApproveID);
                })

            },
            save:function () {
                this.form.FNeedCost = this.totalprice
                this.form.listdata = JSON.stringify(this.listdata);
                this.form.listsupple1 = JSON.stringify(this.listsupple1);
                this.form.listsupple0 = JSON.stringify(this.listsupple0);
                util.post("server/business/ConstructionCost.ashx?cmd=Save",this.form,this.switchLoading,result=> {
                    util.msgSuc("保存成功")
                    if(!this.ApproveID) {
                        this.editFormVisible = false
                        this.$emit("onSaveSuc", this.form)
                    }
                })
            },
            changesubitem(val,record,row){
                row.FItem1=''
                row.FItem2=''
                row.FItem3=''
                row.FItem4=''
                row.FItem5=''
                row.FItem6=''
                row.FItem7=''
                row.FItem8=''
                row.FItem9=''
                row.FItem10=''
            },
            addRow(){
                this.listdata.push({FSubitemID: '',FItem1:'',FItem2: '',FItem3:'',FItem4:'',FItem5:'',
                    FItem6:'',FItem7:'',FItem8:'',FItem9:'',FItem10:''})
            },
            addsupple1Row(){
                this.listsupple1.push({FID: '0',FName:'',FCost: '',FType:'1'})
            },
            addsupple0Row(){
                this.listsupple0.push({FID: '0',FName:'',FCost: '',FType:'0'})
            },
            delRow(index){
                this.listdata.splice(index,1);
                if(this.listdata.length==0)
                {
                    this.listdata.push({FSubitemID: '-1',FItem1:'',FItem2: '',FItem3:'',FItem4:'',FItem5:'',
                        FItem6:'',FItem7:'',FItem8:'',FItem9:'',FItem10:''})
                }
            },
            delsupple1Row(index){
                this.listsupple1.splice(index,1);
                if(this.listsupple1.length==0)
                {
                    this.listsupple1.push({FID: '0',FName:'',FCost: '',FType:'1'})
                }
            },
            delsupple0Row(index){
                this.listsupple0.splice(index,1);
                if(this.listsupple0.length==0)
                {
                    this.listsupple0.push({FID: '0',FName:'',FCost: '',FType:'0'})
                }
            },
            onApproveSuc()
            {
                global.bus.$emit('RefreshMsgCount')
                this.editFormVisible = false
                this.$emit("onSaveSuc", this.form)
            },
            changeproject(val){
                this.listdata=[]
                this.listdata.push({FSubitemID: '-1',FItem1:'',FItem2: '',FItem3:'',FItem4:'',FItem5:'',
                    FItem6:'',FItem7:'',FItem8:'',FItem9:'',FItem10:''})
                this.listsupple1=[]
                this.listsupple1.push({FID: '0',FName:'',FCost: '',FType:'1'})
                this.listsupple0=[]
                this.listsupple0.push({FID: '0',FName:'',FCost: '',FType:'0'})
                util.get("server/business/Single.ashx?cmd=GetList",
                    {where:"FProjectID="+val,start:"0",limit:'10'},this.switchLoading,result=>{
                        this.columnslist = result.data
                    })
            }
        },
        mounted() {

        }
    }
</script>
